package system.service;

import com.jfinal.aop.Duang;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import org.apache.shiro.SecurityUtils;
import system.dao.ReportDao;
import system.dao.ReportInfoDao;
import system.dao.UserDao;

import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/3/8.
 */
public class ReportService {


    ReportDao reportDao = Duang.duang(ReportDao.class);
    ReportInfoDao reportInfoDao = Duang.duang(ReportInfoDao.class);
    UserDao userDao = Duang.duang(UserDao.class);

    public void getReport(String reportName, int period){
        getReport(reportDao.getReportidByName(reportName), period);
    }
    public List<Record> getReport(int reportid, int period){
        //获取userid
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        int userid = userDao.getIdByName(username);


        List<Record> reportInfo = reportInfoDao.getReportInfoByReportId(reportid);
        for(Record item : reportInfo) {
            String formulaid = item.get("formulaid");
            BigDecimal projectvalue = reportInfoDao.getResultByformulaid(formulaid, period, userid);

            item.set("projectvalue", projectvalue);
        }
        return reportInfo;
    }





    //资产负债表
    public List getZCFZB(int period){
        List<Record> fromulas = Db.find("select * from zcfzb_formula");
        List<Map> zcfzbData = new LinkedList();
        for(Record record : fromulas){
            String formula = record.get("formula");
            //期初余额
            BigDecimal qcye = getYE(period-1, formula);
            //期末余额
            BigDecimal qmye = getYE(period, formula);
            record.set("qcye", qcye.toString());
            record.set("qmye", qmye.toString());

        }
        return fromulas;
    }
    //参数  第几期  计算资产负债表科目公式  获取资产负债表科目的余额
    private BigDecimal getYE(int period, String formula) {
        BigDecimal ye = null;
        Pattern p = Pattern.compile("[\\+\\-＋－]");
        String[] kms = p.split(formula);
        Matcher m=p.matcher(formula);

        for(String km : kms){
            String symbol = "+";
            if(ye != null){
                m.find();
                symbol = m.group();
            }else{
                ye = new BigDecimal("0");
            }
            Record kminfo = Db.findFirst("select * from user_kjkm_period_data where period = ? and kmid = (select kmid from kjkm where name=?)", period, km);
            if(kminfo == null) break;
            BigDecimal jfye = kminfo.get("jfye")!=null? (BigDecimal) kminfo.get("jfye") :new BigDecimal("0");
            BigDecimal dfye = kminfo.get("dfye")!=null? (BigDecimal) kminfo.get("dfye") :new BigDecimal("0");
            if("+".equals(symbol) || "＋".equals(symbol)){
                ye = ye.add(jfye.subtract(dfye));
            }else if("-".equals(symbol) || "－".equals(symbol)){
                ye = ye.subtract(jfye.subtract(dfye));
            }
        }
        return ye;
    }

    //利润表
    public void getLRB(){




    }


    //现金流量表
    public void getXJLLB(){




    }


}
